// pages/build_mytree/build_mytree.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cur_node_path:"",
    cur_node_value:{},
    treeData: {
      text: '档案管理',
      mid:0,
      id: 0,
      type:-1,  //type表示类型 -1是我默认的根节点  0是区域  1是路段 2是灯
      path:'treeData',
      childMenus: []
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.request({
      url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid,  //获取区域列表构造树
      header:header,
      success:function(res){
        // console.log(res, "res");
        //[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let data = JSON.parse(res.data.data);

        //当前没有区域 []
        if(data.length == 0){
          return;
        }

        //当前有区域[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let tmp_distrcit = [];
        data.forEach(function(item, index, self){
          let child_district = {
            text:'',
            mid:'',
            type:'',
            id:'',
            state:''
            // path:'treeData.childMenus['+index+']'
          };
          child_district.text = item.text;
          child_district.mid = item.attributes.mid;
          child_district.type = item.attributes.type;
          child_district.id = item.id;
          child_district.state = item.state;
          tmp_distrcit.push(child_district);
        });

        //把区域信息加入treeData
        let mod_treeData = that.data.treeData;
        mod_treeData.childMenus = tmp_distrcit; 

        that.setData({
          treeData: mod_treeData
        });

        //console.log(that.data.treeData, "treeData");
      },
      fail:function(error){
        console.log(error, "build_mytree loadTree");
      }
    });
  },

  tapItem:function(e){
    //console.log(e, 'tapItem');  //detail: {mid: 1}
    let that = this;
    let item = e.detail.item;
  

    //请求下一级 childMenus
    wx.request({
      url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid+'&id='+item.mid,  //获取区域列表构造树
      header: header,
      success: function (res) {
        // console.log(res, "res");
        //[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let data = JSON.parse(res.data.data);

        //当前没有 []
        if (data.length == 0) {
          return;
        }

        //当前有路段或者灯[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
        let tmp_child = [];
        data.forEach(function (item, index, self) {
          let child = {
            text: '',
            mid: '',
            type: '',
            id: '',
            state: ''
          };
          child.text = item.text;
          child.mid = item.attributes.mid;
          child.type = item.attributes.type;
          child.id = item.id;
          child.state = item.state;
          tmp_child.push(child);
        });

        //把区域信息加入treeData
        // let mod_treeData = that.data.treeData;
        // mod_treeData.childMenus = tmp_distrcit;

        // that.setData({
        //   treeData: mod_treeData
        // });

        //console.log(that.data.treeData, "treeData");
      },
      fail: function (error) {
        console.log(error, "build_mytree loadTree");
      }
    });


    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})